package org.example2;


import java.util.*;


public class IteratorOfIterators implements Iterator<Integer> {

    LinkedList<Iterator<Integer>> list;

    public IteratorOfIterators(List<Iterator<Integer>> a)  {
        list = new LinkedList<>();
        for(Iterator<Integer> iterator: a) {
            if(iterator.hasNext()){
               list.addLast(iterator);
            }
        }

    }

    @Override
    public boolean hasNext() {
        return list.size()>0;
    }

    /**
     *  A [1,3,4,5]
     *  B[]
     *  C[2]
     *
     * @return
     */
    @Override
    public Integer next() {
        if(!hasNext()){
            throw new NoSuchElementException();
        }
        //step1: extract the next element
        Iterator<Integer> nextInterator = list.removeFirst();
        int ans = nextInterator.next();


        //step2 prepare for the next time next fn is called
        if(nextInterator.hasNext()){
            list.addLast(nextInterator);
        }
        //step3: return the next element
        return ans;
    }
}
